package com.heba.greenmulu;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.load.engine.Resource;
import com.githang.statusbar.StatusBarCompat;
import com.heba.ATApp;
import com.heba.Cfg;
import com.heba.adapter.GuidePagerAdapter;
import com.heba.banner.MyViewPager;
import com.heba.banner.ViewPagerFocusView;
import com.heba.bean.ItemGuideObject;
import com.heba.config.CommonConfig;
import com.heba.dialog.CustomDialogView;
import com.heba.greenmulu.login.LoginActivity;
import com.heba.greenmulu.login.SignUpNewActivity;
import com.heba.helper.MemberShipHelper;
import com.heba.utils.NotificationUtils;
import com.heba.utils.StatusBarUtils;
import com.leaf.library.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * GuideActivity 引导页面
 * Created by jason on 2019/2/01.
 */
public class GuideActivity extends BaseActivity implements View.OnClickListener {

    private ImageView imageView;
    private ViewPagerFocusView viewPagerFocusView;
    private MyViewPager viewPager;
    private Button btn_signup;
    private Button btn_signin;
    private Button btn_language;

    private GuidePagerAdapter guidePagerAdapter;

    private List<ItemGuideObject> list = new ArrayList<>();
    private boolean ifStartLunbo = false;    //判断是否轮播

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //替换为原来的主题，在onCreate之前调用
//        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        //设置状态栏透明
//        StatusBarUtil.setLightMode(this);
        StatusBarUtils.setStatusBar(this);
        setContentView(R.layout.activity_guide);
        findViewById();
        initGuideData();
        initView();
        initLunBoView();
        checkNotification();
    }

    @Override
    protected void findViewById() {
        imageView = findViewById(R.id.imageView);
        viewPagerFocusView = findViewById(R.id.viewpger_focusview);
        viewPager = findViewById(R.id.viewPager);
        btn_signup = findViewById(R.id.btn_signup);
        btn_signin = findViewById(R.id.btn_signin);
        btn_language = findViewById(R.id.btn_language);
        btn_signup.setForceDarkAllowed(false);
    }

    @Override
    protected void initView() {

        //设置logo显示
        if (Cfg.Language == 1) {
            imageView.setImageResource(R.drawable.icon_about_us);
        } else if (Cfg.Language == 2) {
            imageView.setImageResource(R.drawable.icon_about_us_ch);
        }

        btn_signin.setOnClickListener(this);
        btn_signup.setOnClickListener(this);
        btn_language.setOnClickListener(this);
    }

    /**
     * 设置推送通知
     */
    private void checkNotification() {
        /**
         * 用戶設置了不允許接收通知
         */
        if (!NotificationUtils.checkNotificationEnable(this)) {
            if (Cfg.FirstSetNotification) {
                new CustomDialogView(new CustomDialogView.OnCustomDialogClickListener() {
                    @Override
                    public void onYesClick() {
                        NotificationUtils.goNotificationSettings(GuideActivity.this);
                        Cfg.save("FirstSetNotification", false);
                    }

                    @Override
                    public void onNoClick() {
                        Cfg.save("FirstSetNotification", false);
                    }
                }).showDialog(this, getResources().getString(R.string.notification),
                        getResources().getString(R.string.notification_tips));
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_signin:
                openActivity(LoginActivity.class);
                break;
            case R.id.btn_signup:
                Bundle bundle = new Bundle();
                bundle.putString(CommonConfig.SIGNUP_TITLE, getResources().getString(R.string.signup));
                openActivity(SignUpNewActivity.class, bundle);
                break;
            case R.id.btn_language:
                if (Cfg.Language == 1) {
                    //英文語言的處理
                    changeAppLanguage(2);
                } else if (Cfg.Language == 2) {
                    //中文語言的處理
                    changeAppLanguage(1);
                }
            default:
                break;
        }
    }

    /**
     * 更改語言
     *
     * @param language 1:英文  2:中文
     */
    private void changeAppLanguage(int language) {
        Cfg.save("Language", language);   //保存語言
        ATApp.setAppLanguage();  //更新語言
        ATApp.updateRefreshContent();  //更新刷新框架語言
        finish();
        Intent intent = new Intent(this, GuideActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    /**
     * 初始化数据
     */
    private void initGuideData() {
        int[] guideImageIds = new int[]{R.drawable.icon_guide_01, R.drawable.icon_guide_02,
                R.drawable.icon_guide_03, R.drawable.icon_guide_04, R.drawable.icon_guide_05};
        int[] guideTitleIds = new int[]{R.string.guide_text_title1, R.string.guide_text_title2,
                R.string.guide_text_title3, R.string.guide_text_title4, R.string.guide_text_title5};
        int[] guideContentIds = new int[]{R.string.guide_text_desc1, R.string.guide_text_desc2,
                R.string.guide_text_desc3, R.string.guide_text_desc4, R.string.guide_text_desc5};

        for (int i = 0; i < guideImageIds.length; i++) {
            ItemGuideObject itemGuideObject = new ItemGuideObject();
            itemGuideObject.setImgUrl("drawable://" + guideImageIds[i]);
            itemGuideObject.setGuideTitle(this.getResources().getString(guideTitleIds[i]));
            itemGuideObject.setGuideContent(this.getResources().getString(guideContentIds[i]));
            list.add(itemGuideObject);
        }
    }

    /**
     * 轮播广告显示
     */
    private void initLunBoView() {
        guidePagerAdapter = new GuidePagerAdapter(getBaseContext(), list);
        viewPager.setAdapter(guidePagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int currentIndex = position % list.size();
                viewPagerFocusView.setCurrentIndex(currentIndex);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        if (!list.isEmpty()) {
            //总的点数
            viewPagerFocusView.setCount(list.size());
            int gbs = 1000 / list.size();
            int totalMax = gbs * list.size();
            viewPager.setCurrentItem(totalMax); // 初始位置在靠近1000的整个整除的数字

            // 当前位置
            int currentIndex = viewPager.getCurrentItem() % list.size();
            viewPagerFocusView.setCurrentIndex(currentIndex);
            startLunbo();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MemberShipHelper.isLogin()) {
            openActivity(MainActivity.class);
            finishActivity();
        }
        if (!list.isEmpty())
            startLunbo();
        int currentNightMode = this.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        if (currentNightMode == 32){
            if (Cfg.Language == 1) {
                imageView.setImageResource(R.drawable.icon_about_us_light);
            } else if (Cfg.Language == 2) {
                imageView.setImageResource(R.drawable.icon_about_us_ch_light);
            }
        }else{
         if (Cfg.Language == 1) {
              imageView.setImageResource(R.drawable.icon_about_us);
         } else if (Cfg.Language == 2) {
             imageView.setImageResource(R.drawable.icon_about_us_ch);
          }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (!list.isEmpty())
            pauseLunbo();
    }

    /**
     * 停止轮播
     */
    public void pauseLunbo() {
        if (viewPager != null && ifStartLunbo) {
            viewPager.stopTimer();
            ifStartLunbo = false;
        }
    }

    /**
     * 开始轮播
     */
    public void startLunbo() {
        if (viewPager != null && !ifStartLunbo) {
            viewPager.startTimer();
            ifStartLunbo = true;
        }
    }
}
